<template>
    <view class="formPage bg-fff">
        <view class="formBox">
            <card-item title="回答问题">
                <view class="formBody top-10">
                    <view class="col-grey font13">请输入文字回复（审核通过后将在平台展示）</view>
                    <textarea class="contentInp top-15 bg-gray" :focus="true" v-model="content"></textarea>
                </view>
            </card-item>
        </view>

        <view class="btnBox bg-fff top-20">
            <button class="nextBtn clearBtn" @click="submit">提交</button>
        </view>
    </view>
</template>
<script>
import { mapState, mapActions } from 'pinia';
import { useUserInfoStore } from '@/store/index';
import { checkLogin } from '@/common/common';
import { replyQuestion } from '@/common/api/forum';

export default {
    data() {
        return {
            questionId: '',
            image: '',
            content: '',
            focus: true,
        }
    },
    computed: {
        ...mapState(useUserInfoStore, ['userInfo'])
    },
    onLoad(options) {
        this.questionId = options.id || '';
        uni.hideShareMenu();
        const that = this;
        uni.$check_and_login(function () {
        })
    },
    methods: {
        ...mapActions(useUserInfoStore, ['setUserInfo']),

        //提交
        async submit() {
            if (!checkLogin()) {
                uni.navigateTo({
                    url: '/pages/login/login'
                })
                return
            }
            if (!this.content.trim()) {
                uni.$u.toast('请输入有效内容')
                return
            }
            uni.showLoading({
                title: '提交中'
            })
            try {
                let { status, detail } = await replyQuestion({
                    content: this.content,
                    questionId: this.questionId
                });
                if (status !== 0) {
                    uni.hideLoading();
                    uni.$u.toast(detail);
                    return;
                }
                uni.hideLoading();
                uni.navigateTo({
                    url: '/pages/tools/question/answer-publish-success'
                })
            } catch (error) {
                uni.hideLoading();
                uni.$u.toast(error);
            }
        }
    }
}
</script>

<style scoped lang="scss">
.formPage {
    min-height: 100vh;
    overflow: hidden;

    .formBox {
        margin: 30rpx;

        .formBody {
            .contentInp {
                padding: 10rpx;
                width: calc(100% - 20rpx);
                height: 20vh;
            }
        }
    }

    .btnBox {
        padding: 28rpx;

        .nextBtn {
            font-size: 28rpx;
            height: 80rpx;
            line-height: 80rpx;
            color: #FFFFFF;
            background-color: $theme-color;
        }
    }
}
</style>
